package etm.servlet.junta;

import java.io.IOException;
import java.sql.Date;
import java.sql.Time;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import upc.condominio.modelo.Junta;
import etm.excepcion.DAOExcepcion;
import etm.negocio.GestionJunta;

/**
 * Servlet implementation class JuntaInsertaServlet
 */
@WebServlet("/JuntaInsertaServlet")
public class JuntaInsertaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JuntaInsertaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Dentro de doPost del servlet JuntaInsertaServlet");
		
		GestionJunta negocio = new GestionJunta();
		Junta j = null;
		try {
			String fecha = request.getParameter("txfecha");
			System.out.println("fecha:"+fecha);
			Date datFecha = Date.valueOf(fecha);
			String hora = request.getParameter("txhora");
			if (hora.length()==5){hora=hora+":00";}
			System.out.println("hora:"+hora);
			Time tmHora = Time.valueOf(hora);
			tmHora.toString();
			
			String strTema=request.getParameter("txasunto");
			
			
			j=negocio.insertarJunta(datFecha,tmHora,strTema);
			request.setAttribute("junta", j);
			request.setAttribute("idjunta", j.getIdJunta());
			request.setAttribute("dtfecha", fecha);
			request.setAttribute("tmhora", hora);
			request.setAttribute("stasunto", strTema);
			request.setAttribute("mensaje", "Se registro la junta con id: " + String.valueOf(j.getIdJunta()) );
			response.sendRedirect(request.getContextPath()	+ "/JuntaEditaServlet?id=" + j.getIdJunta());
//			RequestDispatcher rd = request.getRequestDispatcher("JuntaEdit.jsp");
//			rd.forward(request, response);
			
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}

}
